<!--
 * Description: 专案接收主页面
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/03/31 07:26
-->

<template>
  <BaseWrap>
    <!-- 查询条件 -->
    <BaseFunc
      :is-requesting="isRequesting"
      :query-form="queryForm"
      :visible.sync="advanceOptionDrawer"
      @query="query"
    >
      <!-- 按钮 -->
      <template slot="btn">
        <el-button
          v-if="$hasPer('add')"
          class="btn-custom"
          icon="el-icon-circle-plus-outline"
          plain
          size="mini"
          type="primary"
          @click="addDialogVisible = true"
        >
          {{ $t('button.add') }}
        </el-button>
        <el-button
          v-if="$hasPer('syncData')"
          class="btn-custom"
          :loading="isRequesting"
          size="mini"
          type="warning"
          @click="syncProject"
        >
          {{ $t('button.syncData') }}
        </el-button>
      </template>

      <!--抽屉内部查询表单-->
      <template>
        <el-form class="g-advance-drawer-form" label-width="100px" :model="queryForm" size="medium">
          <el-row>
            <!--专案编号-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.projectId')">
                <el-input v-model="queryForm.projectId" clearable />
              </el-form-item>
            </el-col>

            <!--专案名称-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.projectName')">
                <el-input v-model="queryForm.projectName" clearable />
              </el-form-item>
            </el-col>

            <!--bu类型-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.bu')">
                <el-input v-model="queryForm.bu" clearable />
              </el-form-item>
            </el-col>

            <!--系列-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.series')">
                <el-input v-model="queryForm.series" clearable />
              </el-form-item>
            </el-col>

            <!--PM-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.pm')">
                <el-input v-model="queryForm.pm" clearable />
              </el-form-item>
            </el-col>

            <!--PBM负责人-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.pbmOwner')">
                <el-select
                  v-model="queryForm.pbmOwner"
                  clearable
                  filterable
                  :placeholder="$t('select.placeholder')"
                >
                  <el-option
                    v-for="item in $store.getters['user/getPbmUserSimpleCollects']"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  >
                    <span style="float: left;">{{ item.userName }}</span>
                    <span style="float: right; color: #b4b4b4; font-size: 12px">
                      {{ item.gcmFunction + ' ' + item.team }}
                    </span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!--ME负责人-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.meOwner')">
                <el-select
                  v-model="queryForm.meOwner"
                  clearable
                  filterable
                  :placeholder="$t('select.placeholder')"
                >
                  <el-option
                    v-for="item in $store.getters['user/getMEGcmUserSimpleCollects']"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  >
                    <span style="float: left;">{{ item.userName }}</span>
                    <span style="float: right; color: #b4b4b4; font-size: 12px">
                      {{ item.gcmFunction + ' ' + item.team }}
                    </span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!--EE负责人-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.eeOwner')">
                <el-select
                  v-model="queryForm.eeOwner"
                  clearable
                  filterable
                  :placeholder="$t('select.placeholder')"
                >
                  <el-option
                    v-for="item in $store.getters['user/getEEGcmUserSimpleCollects']"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  >
                    <span style="float: left;">{{ item.userName }}</span>
                    <span style="float: right; color: #b4b4b4; font-size: 12px">
                      {{ item.gcmFunction + ' ' + item.team }}
                    </span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <!--PKG负责人-->
            <el-col :span="8">
              <el-form-item :label="$t('Project.pkgOwner')">
                <el-select
                  v-model="queryForm.pkgOwner"
                  clearable
                  filterable
                  :placeholder="$t('select.placeholder')"
                >
                  <el-option
                    v-for="item in $store.getters['user/getPKGGcmUserSimpleCollects']"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  >
                    <span style="float: left;">{{ item.userName }}</span>
                    <span style="float: right; color: #b4b4b4; font-size: 12px">
                      {{ item.gcmFunction + ' ' + item.team }}
                    </span>
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
    </BaseFunc>

    <!--专案数据表格-->
    <BaseTable
      :is-requesting="isRequesting"
      :page-number.sync="pageNumber"
      :page-size.sync="pageSize"
      :table-data="tableData"
      :total="total"
      @query="query"
    >
      <template slot="header">
        <!--序号列-->
        <el-table-column align="center" fixed :label="$t('table.NO')" min-width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
      </template>

      <template>
        <!--专案编号列-->
        <el-table-column
          align="center"
          :label="$t('Project.projectId')"
          min-width="250px"
          prop="projectId"
          sortable
        />
        <!--专案名称列-->
        <el-table-column
          align="center"
          :label="$t('Project.projectName')"
          min-width="300px"
          prop="projectName"
          show-overflow-tooltip
          sortable
        />
        <!--bu类型列-->
        <el-table-column
          align="center"
          :label="$t('Project.bu')"
          min-width="150px"
          prop="bu"
          sortable
        />

        <!--Subject-->
        <el-table-column align="center" label="Subject" min-width="200px" prop="subject" sortable />

        <!--系列列-->
        <el-table-column
          align="center"
          :label="$t('Project.series')"
          min-width="180px"
          prop="series"
          sortable
        />
        <!--专案PM列-->
        <el-table-column
          align="center"
          :label="$t('Project.pm')"
          min-width="150px"
          prop="pm"
          sortable
        />
        <!--专案PBM负责人列-->
        <el-table-column align="center" :label="$t('Project.pbmOwner')" min-width="150px">
          <template slot-scope="scope">
            <span>
              {{ $store.getters['user/getUserNameById'](scope.row.pbmOwner) || $t('result.none') }}
            </span>
          </template>
        </el-table-column>
        <!--专案ME负责人列-->
        <el-table-column align="center" :label="$t('Project.meOwner')" min-width="150px">
          <template slot-scope="scope">
            <span>
              {{ $store.getters['user/getUserNameById'](scope.row.meOwner) || $t('result.none') }}
            </span>
          </template>
        </el-table-column>
        <!--专案EE负责人列-->
        <el-table-column align="center" :label="$t('Project.eeOwner')" min-width="150px">
          <template slot-scope="scope">
            <span>
              {{ $store.getters['user/getUserNameById'](scope.row.eeOwner) || $t('result.none') }}
            </span>
          </template>
        </el-table-column>
        <!--专案PKG负责人列-->
        <el-table-column align="center" :label="$t('Project.pkgOwner')" min-width="150px">
          <template slot-scope="scope">
            <span>
              {{ $store.getters['user/getUserNameById'](scope.row.pkgOwner) || $t('result.none') }}
            </span>
          </template>
        </el-table-column>
        <!--创建人列-->
        <el-table-column align="center" :label="$t('Project.createUser')" min-width="150px">
          <template slot-scope="scope">
            <span>
              {{
                $store.getters['user/getUserNameById'](scope.row.createUser) || $t('result.unknown')
              }}
            </span>
          </template>
        </el-table-column>
        <!--创建时间列-->
        <el-table-column
          align="center"
          :label="$t('Project.createTime')"
          min-width="250px"
          prop="createTime"
        />
        <!--修改人列-->
        <el-table-column align="center" :label="$t('Project.modifyUser')" min-width="150px">
          <template slot-scope="scope">
            <span>
              {{
                $store.getters['user/getUserNameById'](scope.row.modifyUser) || $t('result.unknown')
              }}
            </span>
          </template>
        </el-table-column>
        <!--修改时间列-->
        <el-table-column
          align="center"
          :label="$t('Project.modifyTime')"
          min-width="250px"
          prop="modifyTime"
        />
      </template>

      <!-- 右浮动 -->
      <template slot="end">
        <!--是否已分配列-->
        <el-table-column
          align="center"
          fixed="right"
          :label="$t('Project.isAssigned')"
          min-width="100px"
        >
          <template slot-scope="scope">
            <span v-if="scope.row.isAssigned" class="tag-green">
              {{ $t('Project.assigned') }}
            </span>
            <span v-else class="tag-red">{{ $t('Project.unAssigned') }}</span>
          </template>
        </el-table-column>

        <!--操作列-->
        <el-table-column
          align="center"
          fixed="right"
          :label="$t('table.operate')"
          min-width="250px"
        >
          <template slot-scope="scope">
            <el-button
              v-if="$hasPer('view')"
              size="mini"
              type="text"
              @click="rowView(scope.$index, scope.row)"
            >
              <i class="el-icon-view el-icon--right"></i>
              {{ $t('button.view') }}
            </el-button>
            <el-button
              v-if="$hasPer('edit')"
              size="mini"
              type="text"
              @click="rowEdit(scope.$index, scope.row)"
            >
              <i class="el-icon-edit el-icon--right"></i>
              {{ $t('button.edit') }}
            </el-button>
            <el-button
              v-if="$hasPer('delete')"
              size="mini"
              type="text"
              @click="rowDelete(scope.$index, scope.row)"
            >
              <i class="el-icon-delete el-icon--right"></i>
              {{ $t('button.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </template>
    </BaseTable>

    <!--新增对话框-->
    <el-dialog
      v-if="addDialogVisible"
      :append-to-body="true"
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="$t('dialog.add')"
      :visible.sync="addDialogVisible"
      width="60%"
    >
      <project-data-local-add @cancel="addDialogVisible = false" @save="afterAddSave" />
    </el-dialog>

    <!--编辑对话框-->
    <el-dialog
      v-if="editDialogVisible"
      :append-to-body="true"
      center
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="$t('dialog.edit')"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <project-data-local-edit
        :input-data="currentEditRow"
        @cancel="editDialogVisible = false"
        @save="afterEditSave"
      />
    </el-dialog>

    <!--查看对话框-->
    <el-dialog
      v-if="viewDialogVisible"
      :append-to-body="true"
      center
      :title="$t('dialog.view')"
      :visible.sync="viewDialogVisible"
      width="70%"
    >
      <project-data-local-view :input-data="currentEditRow" />
    </el-dialog>
  </BaseWrap>
</template>

<script>
import { projectPageList, projectDelete } from '../../../api/quote/project'
import ProjectDataLocalAdd from './components/ProjectDataLocalAdd'
import ProjectDataLocalEdit from './components/ProjectDataLocalEdit'
import ProjectDataLocalView from './components/ProjectDataLocalView'
import { syncIdpProject } from '@/api/schedule/schedule'

export default {
  name: 'ProjectDataLocal',
  components: { ProjectDataLocalView, ProjectDataLocalEdit, ProjectDataLocalAdd },
  props: {},
  data() {
    return {
      // 正在查询
      isRequesting: false,
      // 高级查询选项抽屉弹出标识
      advanceOptionDrawer: false,
      // 页码
      pageNumber: 1,
      // 页展示行数
      pageSize: 10,
      // 总行数
      total: 0,
      // 表格数据
      tableData: [],
      // 查询表单对象
      queryForm: {
        projectId: '',
        projectName: '',
        series: '',
        bu: '',
        pm: '',
        pbmOwner: '',
        meOwner: '',
        eeOwner: '',
        pkgOwner: ''
      },
      // 新增对话框可见标识
      addDialogVisible: false,
      // 编辑对话框可见标识
      editDialogVisible: false,
      // 查看对话框可见标识
      viewDialogVisible: false,
      // 当前编辑行的数据
      currentEditRow: {}
    }
  },
  computed: {},
  watch: {},
  created() {
    this.pageDataInit()
  },
  methods: {
    /* 页面数据初始化*/
    pageDataInit() {
      this.query()
    },
    /* 查询表格数据*/
    async query() {
      try {
        this.isRequesting = true
        this.advanceOptionDrawer = false
        const res = await projectPageList({
          pageSize: this.pageSize,
          pageNumber: this.pageNumber,
          pageCondition: this.queryForm
        })
        this.tableData = res.data.list
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    rowView(index, row) {
      this.currentEditRow = row
      this.viewDialogVisible = true
    },
    /* 行记录编辑*/
    rowEdit(index, row) {
      this.currentEditRow = row
      this.editDialogVisible = true
    },
    /* 行记录删除 */
    rowDelete(index, row) {
      this.$confirmBox(this.$t('button.delete'), 'error')
        .then(() => {
          projectDelete({ projectId: row.projectId }, { msgSuccess: true, msgError: true }).then(
            () => {
              this.query()
            }
          )
        })
        .catch(cancel => cancel)
    },
    // 新增保存之后
    afterAddSave() {
      this.addDialogVisible = false
      this.query()
    },
    // 修改保存之后
    afterEditSave() {
      this.editDialogVisible = false
      this.query()
    },
    /** 手动同步专案数据 */
    async syncProject() {
      try {
        this.isRequesting = true
        const res = await syncIdpProject({})
        this.$message({
          message: '同步 ' + res.data + ' 条数据',
          type: 'success',
          duration: 1800
        })
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    }
  }
}
</script>

<style scoped></style>
